package com.coding.controller;

import com.coding.base.pojo.BaseModel;
import com.coding.base.pojo.PageQueryModel;
import com.coding.service.NoteService;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @auther wangchenghao
 * @date 2020/9/26
 **/
@Controller
@ResponseBody
@RequestMapping("note")
public class NoteController {
    @Autowired
    NoteService noteService;

    @ApiOperation(value = "通过id查询笔记")
    @RequestMapping(value = "/getById", method = {RequestMethod.GET})
    public BaseModel getById(@ApiParam(value = "note_id", required = true) @RequestParam("note_id") int note_id) {
        BaseModel baseModel = new BaseModel();
        try {
            noteService.selectNote(note_id, baseModel);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return baseModel;
    }

    @ApiOperation(value = "通过用户查询笔记")
    @RequestMapping(value = "/getByUser", method = {RequestMethod.GET})
    public BaseModel getByUser(@ApiParam(value = "user_id", required = true) @RequestParam("user_id") int user_id,
                               @ModelAttribute PageQueryModel pageQueryModel) {
        BaseModel baseModel = new BaseModel();
        try {
            noteService.selectByUser(user_id, pageQueryModel, baseModel);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return baseModel;
    }

    @ApiOperation(value = "新增用户笔记")
    @RequestMapping(value = "/addNewNote", method = {RequestMethod.POST})
    public BaseModel addNewNote(@ApiParam(value = "user_id", required = true) @RequestParam("user_id") int user_id,
                                @ApiParam(value = "note_name", required = true) @RequestParam("note_name") String note_name,
                                @ApiParam(value = "note_content", required = true) @RequestParam("note_content") String note_content,
                                @ApiParam(value = "note_place", required = true) @RequestParam("note_place") String note_place
    ) {
        BaseModel baseModel = new BaseModel();
        try {
            noteService.addNote(user_id, note_name, note_content, note_place, baseModel);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return baseModel;
    }
}
